package com.wolfking.converter.spring;

import com.wolfking.converter.service.JdbcTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * converter interface的动态代理的工厂类
 * 经过隔壁老王处理的JDK动态代理的工厂类
 * spring的加载路径，先调用ApplicationContextAware的setApplicationContext,
 * FactoryBean 是注入的时候，调用getObject
 *
 * @author 赵伟伟(wolfking)
 * created on 2019-09-29 13:33
 * Email is zhaoweiwei@youxin.com
 * Copyright is 友信金服
 */
@Slf4j
class ConverterProxyFactory implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {
    /*通过构造方法注入设置*/
    private final Class<?> converterInterface;

    @Autowired
    private JdbcTemplateService jdbcTemplateService;

    ConverterProxyFactory(Class<?> converterInterface) {
        this.converterInterface = converterInterface;
    }

    @Override
    @SuppressWarnings("all")
    public Object getObject() throws Exception {
        long start = System.currentTimeMillis();
        log.info("ConverterProxyFactory create the JDK Dynamic Proxy class ,{}", converterInterface.getName());
        Object obj = new JdkConverterProxy(converterInterface, jdbcTemplateService).getConverterProxy();
        log.info("{} create JDK Dynamic Proxy class use {} ms", converterInterface.getName(), System.currentTimeMillis() - start);
        return obj;

    }

    @Override
    public Class<?> getObjectType() {
        return converterInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet() {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    }
}
